﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Ryder_US_Website.BUS;
using Ryder_US_Website.Utils;

namespace Ryder_US_Website.Presentation.Admin
{
    public partial class Customer_Update : System.Web.UI.Page
    {
        public string id;
        RUS_Customer cus = new RUS_Customer();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["RUS_User"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            id=Request.QueryString["id"];
            int temp = Int16.Parse(id);
            cus = CustomerBUS.GetCustomerByID(temp);
            if (!IsPostBack)
            {
                    txtTenKhachHang.Text = cus.Customer_name;
                    txtTenDangNhap.Text = cus.Customer_username;
                    txtKhoanNo.Text = cus.Customer_doubt.ToString();
                    txtDiaChi.Text = cus.Customer_address;
                    txtDienThoai.Text = cus.Customer_phone;
                    txtTaiKhoan.Text = cus.Customer_banking;
                    rbChuaXoa.Checked = (cus.Customer_trash == 0);
                    rbDaXoa.Checked = !rbChuaXoa.Checked;
            }

        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("Customer_List.aspx");
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            RUS_Customer kq = new RUS_Customer();
            string temp = txtTenKhachHang.Text;
            cus.Customer_trash = (rbChuaXoa.Checked == true) ? 0 : 1;
            cus.Customer_name = txtTenKhachHang.Text;
            cus.Customer_username = txtTenDangNhap.Text;
            cus.Customer_doubt = decimal.Parse(txtKhoanNo.Text);
            cus.Customer_banking = txtTaiKhoan.Text;
            cus.Customer_address = txtDiaChi.Text;
            cus.Customer_phone = txtDienThoai.Text;
            try
            {
                if (chbMatKhau.Checked == false)
                {
                    kq = CustomerBUS.Update(cus.Customer_ID, cus.Customer_name, cus.Customer_username, cus.Customer_address, cus.Customer_phone, cus.Customer_banking, cus.Customer_doubt, cus.Customer_trash);

                }
                else
                {
                    if (txtMatKhau.Text == "")
                    {
                        Response.Write(UtilHelpers.ShowMessage("Mời bạn nhập mật khẩu"));
                        chbMatKhau.Checked = false;
                    }
                    else
                    {
                        cus.Customer_password = txtMatKhau.Text;
                        kq = CustomerBUS.Update(cus.Customer_ID, cus.Customer_name, cus.Customer_username, cus.Customer_password, cus.Customer_address, cus.Customer_phone, cus.Customer_banking, cus.Customer_doubt, cus.Customer_trash);
                    }
                }
            }
            catch(Exception ex)
            {
                Response.Write(UtilHelpers.ShowMessage(ex.Message));
            }
            if (cus.Customer_ID.Equals(kq.Customer_ID))
                Response.Redirect("Customer_List.aspx");
            else
                Response.Write(UtilHelpers.ShowMessage("Cập nhật không thành công"));
            
        }
    }
}
